from gideon.service.starlabs import GideonHandler


class PrivateCustomerHandler(GideonHandler):
    display_list = ["checkbox", "name", "qq", "class_status", "gender", "source", "education", "graduation_school",
                    "major", "experience", "work_status", "company", "record", "pay_record"]

    def action_multi_remove(self, title=False, url=None, data=None, *args, **kwargs):
        """
        批量移除私户账户到公户中
        :param title: 显示标题标志
        :param url: 返回成功跳转的url
        :param args:
        :param kwargs:
        :return:
        """
        if data is None:
            data = dict()
        # 不考虑多用户的情况
        current_user_id = self.request.session.get("oracle_users_list")
        if current_user_id:
            current_user_id = current_user_id[0]["id"]
        else:
            return None
        if title:
            return '批量移除到公户中'
        else:
            # 选择客户的key的列表
            key_list = self.request.POST.getlist('key')

            if not url:
                url = self.request.get_full_path()
            status = "success"
            data.update(
                {
                    "head_title": "批量移除成功",
                    "title": "批量移除私户用户到公户中成功",
                    "href": url,
                    "button_text": "返回列表页"
                }
            )
            self.model_class.objects.filter(key__in=key_list, consultant_id=current_user_id).update(consultant=None)
        return status

    def get_action_list(self, action_list=None, *args, **kwargs):
        action_list = super().get_action_list(action_list, *args, **kwargs)
        action_list.append(self.action_multi_remove)
        return action_list

    def get_queryset(self, handle_type='list', *args, **kwargs):
        # 不考虑多用户情况
        current_user_id = self.request.session.get("oracle_users_list")
        if current_user_id:
            current_user_id = current_user_id[0]["id"]
        objects = self.model_class.objects.filter(consultant_id=current_user_id)
        if handle_type == "list":
            return objects
        else:
            return objects.first()

    def saving_pre_handler(self, form, is_update=False, *args, **kwargs):
        if not is_update:
            current_user_id = self.request.session.get("oracle_users_list")
            if current_user_id:
                current_user_id = current_user_id[0]["id"]
                form.instance.consultant_id = current_user_id
        return super().saving_pre_handler(form, is_update=False, *args, **kwargs)

    def show_record_field(self, obj=None, is_title=False, *args, **kwargs):
        """
        跟进记录显示
        :param obj:
        :param is_title:
        :param args:
        :param kwargs:
        :return:
        """
        title = "跟进记录"
        if is_title:
            return title
        if obj:
            url = self.reverse_handle_url(self.request, "supergirl_consultrecord_list", customer_key=obj.key)
            return {"href": url, "title": title}, "body", {"type": "link", "target": "_blank"}
        return "record function error"

    def show_pay_record_field(self, obj=None, is_title=False, *args, **kwargs):
        """
        显示缴费记录
        :param obj:
        :param is_title:
        :param args:
        :param kwargs:
        :return:
        """
        title = "缴费"
        if is_title:
            return title
        if obj:
            url = self.reverse_handle_url(self.request, "supergirl_paymentrecord_list", customer_key=obj.key)
            return {"href": url, "title": title}, "body", {"type": "link", "target": "_blank"}
        return "pay record function error"
